<?php
 
class Imhonet extends CApplicationComponent
{
	/**
	 * Path to Imhonet entry point
	 * @var string
	 */
	public $apiUrl;
	
	private $_authParams;
	
    public function init()
    {
    	parent::init();
    }
 
	public function run($action, $params = array())
    {
    	$params['auth_params'] = $this->getAuthParams();
    	$params['mode'] = 'json';

//    	$data = Yii::app()->CURL->run($this->getActionPath($action), false, $params);
//    	var_dump($data);die;
    	
    	return ($data = Yii::app()->CURL->run($this->getActionPath($action), false, $params)) ?
    		 $mode == 'xml' ? new SimplexmlElement($data) : json_decode($data) :
    		null;
	}
	
	public function setAuthParams($authParams = false)
	{
		$this->_authParams = $authParams ? $authParams : Yii::app()->user->vkSession;
	}

	private function getAuthParams()
	{
		if (empty($this->_authParams))
			$this->setAuthParams();
		
		return $this->_authParams;
	}
	
	private function getActionPath($action)
	{
		return $this->apiUrl.ucfirst(strtolower($action)).'.pxml';
	}
}